package java44th.group.service.admin.keyword;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import java44th.group.bean.common.BasePageInfo;
import java44th.group.bean.common.CommonData;
import java44th.group.bean.po.MarketKeyword;
import java44th.group.bean.po.MarketKeywordExample;
import java44th.group.bean.admin.vo.keyword.CreateKeywordVo;
import java44th.group.mapper.MarketKeywordMapper;
import java44th.group.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;

/**
 * @author 小焦
 * @since 2022/10/13 00:30
 */

@Service
public class KeywordServiceImpl implements KeywordService {

    @Autowired
    MarketKeywordMapper keywordMapper;

    /**
     * 查询订单记录
     *
     * @param info    *  分页:page/limit/  排序:sort/WxOrderController,借助example
     * @param keyword
     * @param url
     * @return java44th.group.bean.common.CommonData<java44th.group.bean.po.MarketKeyword>
     * @author 小焦
     * @since 2022/10/13 上午 11:56
     */
    @Override
    public CommonData<MarketKeyword> list(BasePageInfo info, String keyword, String url) {
        // 分页插件
        PageHelper.startPage(info.getPage(), info.getLimit());

        MarketKeywordExample example = new MarketKeywordExample();
        example.setOrderByClause(info.getSort() + " " + info.getOrder());
        MarketKeywordExample.Criteria criteria = example.createCriteria();
        if (!StringUtil.isEmpty(keyword)) {
            criteria.andKeywordLike("%" + keyword + "%");
        }
        if (!StringUtil.isEmpty(url)) {
            criteria.andUrlLike("%" + url + "%");
        }
        criteria.andDeletedEqualTo(false);
        List<MarketKeyword> list = keywordMapper.selectByExample(example);
        PageInfo<MarketKeyword> pageInfo = new PageInfo<>(list);


        return CommonData.data(pageInfo);
    }

    @Override
    public CreateKeywordVo create(MarketKeyword marketKeyword) {
        if (marketKeyword.getIsHot() == null) {
            marketKeyword.setIsHot(false);
        }
        if (marketKeyword.getIsDefault() == null) {
            marketKeyword.setIsDefault(false);
        }
        if (marketKeyword.getUrl() == null) {
            marketKeyword.setUrl("");
        }
        marketKeyword.setAddTime(new Timestamp(System.currentTimeMillis()));
        marketKeyword.setUpdateTime(new Timestamp(System.currentTimeMillis()));
        marketKeyword.setDeleted(false);
        int count = keywordMapper.insertSelective(marketKeyword);
        CreateKeywordVo keywordVo = new CreateKeywordVo();
        keywordVo.setId(marketKeyword.getId());
        keywordVo.setKeyword(marketKeyword.getKeyword());
        keywordVo.setUrl(marketKeyword.getUrl());
        keywordVo.setHot(marketKeyword.getIsHot());
        keywordVo.setDefault(marketKeyword.getDeleted());
        keywordVo.setAddTime(marketKeyword.getAddTime());
        keywordVo.setUpdateTime(marketKeyword.getUpdateTime());
        return keywordVo;
    }

    @Override
    public int delete(MarketKeyword keyword) {
        keyword.setDeleted(true);
        keyword.setUpdateTime(new Timestamp(System.currentTimeMillis()));
        int count = keywordMapper.updateByPrimaryKeySelective(keyword);
        if (count == 1) {
            return count;
        }
        return 0;
    }

    @Override
    public int update(MarketKeyword keyword) {
        int count = keywordMapper.updateByPrimaryKeySelective(keyword);
        if (count == 1) {
            return count;
        }
        return 0;
    }
}
